package com.qiwo.api.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.domain.UserInfoDO;
import com.qiwo.common.utils.HttpContextUtils;
import com.qiwo.common.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Copyright
 *
 * @Author:chenwenchao
 * @Date:2018/10/6 0006
 * Description:
 */
public abstract class BaseController {
    /**
     * <pre>
     * 自动获取分页参数，返回分页对象page
     * </pre>
     *
     * <small> 2018年4月15日 | Aron</small>
     *
     * @param e
     * @return
     */
    public <E> Page<E> getPage(Class<E> e) {
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        return new Page<>(pageNumber, pageSize);
    }


    private int getParaToInt(String key, int defalut) {
        String pageNumber = HttpContextUtils.getHttpServletRequest().getParameter(key);
        if (StringUtils.isBlank(pageNumber)) {
            return defalut;
        }
        return Integer.parseInt(pageNumber);
    }

    public UserInfoDO getInfoUser() {
        return ShiroUtils.getInfoUser();
    }

    public Long getInfoUserId() {
        return getInfoUser().getId();
    }

}
